package com.mallcloud.mall.product.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mallcloud.mall.common.core.util.R;
import com.mallcloud.mall.common.core.util.ValidGroup;
import com.mallcloud.mall.common.excel.annotation.ResponseExcel;
import com.mallcloud.mall.product.api.entity.SkuModel;
import com.mallcloud.mall.product.api.vo.SkuVO;
import com.mallcloud.mall.product.service.SkuService;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "sku信息管理")
@RestController
@RequiredArgsConstructor
@RequestMapping("/product/sku")
public class SkuController {
    private final SkuService skuService;

    @ApiOperation("分页查询")
    @GetMapping("/page")
    @PreAuthorize("@pms.hasPermission('product_sku_view')")
    public R<IPage<SkuVO>> page(Page page, SkuModel dto) {
        return R.ok(skuService.pageSku(page, dto));
    }

    @ApiOperation("列表查询")
    @GetMapping("/list")
    @PreAuthorize("@pms.hasPermission('product_sku_view')")
    public R<List<SkuModel>> list(SkuModel dto) {
        return R.ok(skuService.list(Wrappers.query(dto)));
    }

    @ApiOperation("通过id查询")
    @GetMapping("/getInfo")
    @PreAuthorize("@pms.hasPermission('product_sku_view')")
    public R<SkuVO> getInfo(SkuModel dto) {
        return R.ok(skuService.getSku(dto.getSkuId()));
    }

    @ApiOperation("新增")
    @PostMapping("/save")
    @PreAuthorize("@pms.hasPermission('product_sku_add')")
    public R<Boolean> save(@Validated({ ValidGroup.Insert.class }) @RequestBody SkuVO dto) {
        return R.ok(skuService.saveSku(dto));
    }

    @ApiOperation("修改")
    @PostMapping("/update")
    @PreAuthorize("@pms.hasPermission('product_sku_edit')")
    public R<Boolean> updateById(@Validated({ ValidGroup.Update.class }) @RequestBody SkuVO dto) {
        return R.ok(skuService.updateSku(dto));
    }

    @ApiOperation("删除")
    @DeleteMapping("/{skuId}")
    @PreAuthorize("@pms.hasPermission('product_sku_del')")
    public R<Boolean> removeById(@PathVariable Integer skuId) {
        return R.ok(skuService.removeById(skuId));
    }

    @ApiOperation("批量删除sku信息")
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('product_sku_del')")
    public R<Boolean> removeByIds(@RequestBody List<Long> skuIds) {
        return R.ok(skuService.removeByIds(skuIds));
    }

    @ApiOperation("导出")
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('product_sku_export')")
    public List<SkuModel> export(SkuModel sku) {
        return skuService.list(Wrappers.query(sku));
    }
}
